function HLS = WriteHlsMap(params, fname, max, tune_max)

% hue: preferred angle
% intenisity: max_change
% saturation: tune (0-tune_max)
% HLS_hc: high contrast HLS map

global absScalingResp;
global absScalingTune;
global maxResp;
global maxTune;

if absScalingResp
    max = maxResp
end
if absScalingTune
    tune_max = maxTune
end

global colorShift;

dim=size(params.th);
HLS=zeros(dim(1),dim(2),3);
HLS_hc=zeros(dim(1),dim(2),3);

params.th = mod(params.th + colorShift, 1 );
if (max > 0)
    for x=1:dim(1)
        for y=1:dim(2)
            m1=params.max_change(x,y)./max;
            m2=params.max_change(x,y)*2./max;
            s=params.tune(x,y)./tune_max;
            if m1 > 1; m1 = 1; end;
            if m2 > 1; m2 = 1; end;
            if s > 1; s = 1; end;
            HLS(x,y,:)=HsvToRgb(params.th(x,y),s,m1)';
            HLS_hc(x,y,:)=HsvToRgb(params.th(x,y),s,m2)';
            %         HLS(x,y,:)=hsv2rgb([params.th(x,y),s,m1]);
            %         HLS_hc(x,y,:)=hsv2rgb([params.th(x,y),s,m2]);
        end
    end
end

imwrite(HLS, [fname, 'HLS.tif']);
imwrite(HLS_hc, [fname, 'HLS_hc.tif']);
